<template>
    <view class="container">
        <image class="bg-image" src="/static/image.jpg" mode="aspectFill"></image>
        <view class="nav-bar">
            <view class="back-btn" @click="goBack">
                <image src="/static/return.png" mode="aspectFit" class="back-icon"/>
            </view>
            <text class="nav-title">阅读文章</text>
        </view>
        
        <view class="content">
            <view class="card-body">
                <view 
                    class="article-item" 
                    v-for="article in articles" 
                    :key="article.article_id"
                    @click="goToDetail(article)"
                >
                    <view class="article-info">
                        <text class="article-title">{{ article.article_title }}</text>
                        <text class="article-date">{{ formatDate(article.create_time) }}</text>
                    </view>
                </view>
            </view>
        </view>
        
        <view class="upload-btn" @click="goToUpload">
            <text class="upload-text">上传文章</text>
        </view>
    </view>
</template>

<script>
import { get_approved_articles } from '@/api/user.js'

export default {
    data() {
        return {
            articles: []
        }
    },
    async onLoad() {
        await this.loadArticles()
    },
    methods: {
        async loadArticles() {
            try {
                const res = await get_approved_articles()
                console.log('获取到的文章列表：', res)
                if (res.code === '2000') {
                    this.articles = res.data
                }
            } catch (error) {
                console.error('获取文章列表失败：', error)
            }
        },
        goToDetail(article) {
            console.log('传递的文章数据：', article)
            // 存储完整的文章数据
            uni.setStorageSync('currentArticle', {
                article_title: article.article_title,
                article_content: article.article_content,
                create_time: article.create_time
            })
            uni.navigateTo({
                url: `/pages/article/detail?id=${article.article_id}`
            })
        },
        goBack() {
            uni.navigateBack()
        },
        formatDate(dateStr) {
            const date = new Date(dateStr)
            return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
        },
        goToUpload() {
            uni.navigateTo({
                url: '/pages/article/upload'
            })
        }
    }
}
</script>

<style>
.container {
    position: relative;
    min-height: 100vh;
}

.bg-image {
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    z-index: -1;
}

.nav-bar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    display: flex;
    align-items: center;
    padding: 40px 20px 20px;
    z-index: 100;
}

.back-btn {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.back-icon {
    width: 24px;
    height: 24px;
}

.nav-title {
    flex: 1;
    text-align: center;
    font-size: 18px;
    font-weight: 500;
    color: #333;
	
    text-shadow: 0 1px 2px rgba(255, 255, 255, 0.5);
}

.content {
    padding: 100px 20px 20px;
    min-height: 100vh;
	margin-top: 100px;
}

.card-body {
    background: rgba(255, 255, 255, 0.2);
    backdrop-filter: blur(10px);
    border-radius: 12px;
    padding: 20px;
}

.article-item {
    padding: 15px;
    margin-bottom: 15px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 8px;
    transition: all 0.3s ease;
}

.article-item:active {
    background: rgba(255, 255, 255, 0.2);
}

.article-info {
    display: flex;
    flex-direction: column;
}

.article-title {
    font-size: 16px;
    color: #333;
    margin-bottom: 8px;
    font-weight: 500;
}

.article-date {
    font-size: 12px;
    color: #666;
}

.upload-btn {
    position: fixed;
    bottom: 30px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(107, 75, 138, 0.9);
    backdrop-filter: blur(5px);
    padding: 12px 30px;
    border-radius: 24px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    z-index: 100;
}

.upload-text {
    color: #fff;
    font-size: 16px;
    font-weight: 500;
}

.upload-btn:active {
    opacity: 0.8;
}
</style> 